package com.gxl.butt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.gxl.butt.dto.bo.Topic;
import com.gxl.butt.dto.qo.ParamBean;
import com.gxl.butt.service.IkafkaOperatorService;
import com.gxl.butt.utils.HttpUtils;
import com.gxl.butt.utils.JSONUtils;
import com.gxl.butt.value.ErrorMsg;
import com.gxl.butt.value.StaticValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import static com.gxl.butt.value.ErrorMsg.*;
import static com.gxl.butt.value.ErrorMsg.FAILD_MSG;
import static com.gxl.butt.value.ErrorMsg.STR_IS_EMPTY;

/**
 * Created by guoxiaolin on 2018/7/19.
 */

@Api(value = "api",tags = "API")
@RestController
@RequestMapping(value = "/api")
public class ApiController {
    private Logger log = LoggerFactory.getLogger(ApiController.class);

    @Reference(interfaceClass = IkafkaOperatorService.class,timeout = 10000,check = false)
    private IkafkaOperatorService kafkaOperatorService;


    @ApiOperation(value="发送消息", notes="发送消息到指定topic中")
    @RequestMapping(value = "/ButtNotice",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public JSONObject ButtNotice(HttpServletRequest request, HttpServletResponse response){

        Topic topic = new Topic();
        topic.setTopicname(request.getParameter("topic.topicname"));
        topic.setOpenid(request.getParameter("topic.openid"));
        topic.setData(JSONObject.parseObject(request.getParameter("topic.data")));
        topic.setToken(request.getParameter("topic.token"));

        JSONObject jsonObject = kafkaOperatorService.sendMes(topic);
        Integer code = jsonObject.getInteger(JSONUTIL_ERROR_CODE);
        if (code==0){
            return  JSONUtils.getRoot(0,SUCCESS_MSG, STR_IS_EMPTY);
        }
        if (code==1){
            return  JSONUtils.getRoot(1,TOPIC_NOT_EXIST, STR_IS_EMPTY);
        }
        return JSONUtils.getRoot(1,FAILD_MSG, STR_IS_EMPTY);

    }

    @ApiOperation(value="聊天机器人接口", notes="聊天机器人接口")
    @RequestMapping(value = "/robootsay",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public JSONObject robootsay(HttpServletRequest request, HttpServletResponse response,@RequestBody ParamBean msg){

        Map<String,String> data = new HashMap<String ,String>();
        data.put("msg",msg.getObj().getString("msg"));
        data.put("uid","drea_bbs_chat");
        String resj =  HttpUtils.sendPostgbk(ErrorMsg.ROBOOTURL,data);

        JSONObject jsondata = JSONObject.parseObject(resj);

        return jsondata;

    }

    @ApiOperation(value="getwather", notes="天气预报")
    @RequestMapping(value = "/getwather",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public JSONObject getwather(HttpServletRequest request, HttpServletResponse response,@RequestBody ParamBean msg){

        Map<String,String> data = new HashMap<String ,String>();
        data.put("city",msg.getObj().getString("city"));
        String resj = HttpUtils.sendPost(ErrorMsg.WATHERURL,data);

        JSONObject jsondata = JSONObject.parseObject(resj);

        return jsondata;

    }


    @ApiOperation(value="wxGetOpenid", notes="获取微信小程序openid")
    @RequestMapping(value = "/wxGetOpenid",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public JSONObject wxGetOpenid(HttpServletRequest request, HttpServletResponse response,@RequestBody ParamBean msg){

        Map<String,String> data = new HashMap<String ,String>();
        data.put("appid", StaticValue.WX_APPID);
        data.put("secret", StaticValue.WX_SECRET);
        data.put("js_code", msg.getObj().getString("js_code"));
        data.put("grant_type", "authorization_code");
        String resj = HttpUtils.sendPost(StaticValue.WX_GET_OPENID, data);

        JSONObject jsondata = JSONObject.parseObject(resj);

        return jsondata;

    }
}
